package com.hibernate.hrm.entity;

import com.fasterxml.jackson.annotation.*;
import jakarta.persistence.*;
import lombok.Data;
import lombok.ToString;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "department")
@Data
@ToString(exclude = {"departmentHeadID", "departmentParentID", "subDepartments"})
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "departmentID"
)
public class Department {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "departmentID")
    private Integer departmentID;

    @Column(name = "departmentName", nullable = false)
    private String departmentName;

    @Column(name = "departmentCode", nullable = false)
    private String departmentCode;

    @Column(name = "departmentIntroduction", columnDefinition = "TEXT")
    private String departmentIntroduction;

    @ManyToOne
    @JoinColumn(name = "departmentHeadID")
    @JsonIgnoreProperties({"departmentID", "jobPositionID"})
    private User departmentHeadID;

    @ManyToOne
    @JoinColumn(name = "departmentParentID")
    @JsonBackReference // 阻止子序列化父
    private Department departmentParentID;

    @Column(name = "departmentLevel")
    private Integer departmentLevel = 1;

    @Column(name = "status", nullable = false)
    private Boolean status = true;

    @Column(name = "createTime", nullable = false)
    private LocalDateTime createTime;

    @Column(name = "updateTime")
    private LocalDateTime updateTime;

    @OneToMany(mappedBy = "departmentParentID")
    @JsonManagedReference // 允许父序列化子
    private List<Department> subDepartments = new ArrayList<>();
}
